package com.example.demo.config;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.HandlerInterceptor;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.JWTUtil;

public class WebInterceptor implements HandlerInterceptor {

  @Override
  public boolean preHandle(HttpServletRequest request, javax.servlet.http.HttpServletResponse response,
      Object handler) throws java.lang.Exception {

    String token = request.getHeader("token");

    boolean valid = JWTUtil.verify(token);

    if (valid) {
      return true;
    } else {
      JSONObject json = new JSONObject();
      json.put("code", 403);

      response.getWriter().write(json.toJSONString());

      return false;
    }
  }

}
